package com.toogu.mazhangmen.ui.main;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.ui.view.MonitorView;
import com.toogu.mazhangmen.ui.view.ReSpinner;
import com.toogu.mazhangmen.util.MonitorUtil;
import com.toogu.mazhangmen.util.PopUtil;
import com.toogu.mazhangmen.util.UiUtil;

public class MonitorWithButton extends LinearLayout {

    private MonitorView monitorView;
    public MonitorUtil monitorUtil;
    private TextView controlCall;
    public TextView controlMore;
    private TextView doorName;
    public ReSpinner reSpinner;

    public EquipmentsBean equipmentsBean;
    public int doorPosition;

    private static final int Alarm = 1;
    private static final int CloseDoor = 2;
    private static final int OpenDoor = 3;
    private static final int Help = 4;
    private static final int Phone = 5;
    private static final int Call = 6;

    private PopupWindow popupWindow;

   public interface ClickLister{
       void buttonTalk(TextView textView);
       void buttonAlarm(TextView textView);
       void closeDoor(String doorId);
       void openDoor(String doorId);
       void closeHelp();
       void openCall();
   }

    public MonitorWithButton(Context context) {
        super(context);
        init(context);
    }

    public MonitorWithButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MonitorWithButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        final View itemView = LayoutInflater.from(context).inflate(R.layout.view_monitor_button, null, false);
        LinearLayout layout = itemView.findViewById(R.id.layout);
        monitorView = itemView.findViewById(R.id.MonitorView);
        reSpinner = itemView.findViewById(R.id.respinner);
        controlCall = itemView.findViewById(R.id.controlCall);
        controlMore = itemView.findViewById(R.id.controlMore);
        doorName= itemView.findViewById(R.id.doorName);
        addView(itemView);
    }
    public void setInit(Activity context, EquipmentsBean equipment, ClickLister clickLister){
        equipmentsBean = equipment;
        initSpinner(context);
        monitorUtil = new MonitorUtil(context,equipmentsBean,monitorView);
        View.OnClickListener  onClickListener = new View.OnClickListener(){
            boolean isTalk = false;
            @Override
            public void onClick(View v) {
                String doorId;
                String doorName;
                if(equipmentsBean.getDoor()!=null) {
                    doorId = equipmentsBean.getDoor().getId();
                    doorName = equipment.getDoor().getName();
                }
                else if(equipmentsBean.getDoors()!=null) {
                    doorId = equipment.getDoors().get(doorPosition).getId();
                    doorName=equipment.getDoors().get(doorPosition).getName();
                }
                else {
                    doorId="-1";
                    doorName = "";
                }

                switch (v.getId()){
                    case R.id.controlCall:
                        clickLister.buttonTalk((TextView)v);
                        break;
                    case Alarm:
                        clickLister.buttonAlarm((TextView)v);
                        break;
                    case CloseDoor:
                        clickLister.closeDoor(doorId);
                        break;
                    case OpenDoor:
                        clickLister.openDoor(doorId);
                        break;
                    case Help:
                        clickLister.closeHelp();
                        break;
                    case Call:
                        clickLister.openCall();
                        break;
                    case Phone:
                        break;
                }
                popupWindow.dismiss();
            }
        };
        popupWindow = createPopupMenu(context,onClickListener);
        controlCall.setOnClickListener(onClickListener);
        controlMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int windowPos[] = PopUtil.calculatePopWindowPos(controlMore, popupWindow.getContentView());
                popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
                ((TextView)v).setText("关闭");
            }
        });
    }

    private void initSpinner(Context context) {
        if(equipmentsBean.getDoor()!=null||equipmentsBean.getDoors()==null) {
            reSpinner.setVisibility(GONE);
            if(equipmentsBean.getDoor()!=null){
                doorName.setVisibility(VISIBLE);
                doorName.setText(equipmentsBean.getDoor().getName());
            }
            return ;
        }

        String[] doorName = new String[equipmentsBean.getDoors().size()];
        for(int i=0;i<equipmentsBean.getDoors().size();i++){
            doorName[i]=equipmentsBean.getDoors().get(i).getName();
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, R.layout.view_spinner_item2, doorName);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        reSpinner.setAdapter(arrayAdapter);
        reSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                doorPosition = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    public PopupWindow createPopupMenu(Activity context,View.OnClickListener clickListener) {
        PopupWindow popupWindow = new PopupWindow(context);
        //设置rootView
        LinearLayout rootView = new LinearLayout(context);
        rootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        rootView.setPadding(UiUtil.dip2px(5), UiUtil.dip2px(5), UiUtil.dip2px(5), UiUtil.dip2px(5));
        rootView.setBackgroundColor(Color.WHITE);
        rootView.setOrientation(LinearLayout.VERTICAL);
        popupWindow.setContentView(rootView);
        //设置弹出的菜单
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, UiUtil.dip2px(2), 0, 0);
        rootView.addView(createButton(context, Alarm, "打开警报", clickListener), layoutParams);
        rootView.addView(createButton(context, CloseDoor, "关门", clickListener), layoutParams);
        rootView.addView(createButton(context, OpenDoor, "开半门(手动)", clickListener), layoutParams);
        rootView.addView(createButton(context, Help, "已处理求助", clickListener), layoutParams);
        rootView.addView(createButton(context, Call, "问题反馈", clickListener), layoutParams);
        rootView.addView(createButton(context, Phone, "手机号：无数据", clickListener), layoutParams);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                controlMore.setText("更多");
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        return popupWindow;
    }

    public TextView createButton(Activity activity,int id, String content, View.OnClickListener onClickListener) {
        TextView button =(TextView) LayoutInflater.from(activity).inflate(R.layout.view_control_button, null);
        button.setId(id);
        button.setText(content);
        button.setOnClickListener(onClickListener);
        return button;
    }

    public MonitorUtil getMonitorUtil(){
        return monitorUtil;
    }

    public MonitorView getMonitorView(){return monitorView;}

}
